using System;

namespace armcontrolgui.RandomLogicArm
{
	[System.ComponentModel.ToolboxItem(true)]
	public partial class AddInfo : Gtk.Bin
	{
		private RLArm rla_CurrentArm;
		private bool bo_DisableEvent;
		public AddInfo (RLArm MyArm)
		{
			this.Build ();
			
			this.XSpinButton.Adjustment.Lower = double.MinValue;
			this.XSpinButton.Adjustment.Upper = double.MaxValue;
			this.YSpinButton.Adjustment.Lower = double.MinValue;
			this.YSpinButton.Adjustment.Upper = double.MaxValue;
			this.ZSpinButton.Adjustment.Lower = double.MinValue;
			this.ZSpinButton.Adjustment.Upper = double.MaxValue;
			this.WristTableSpinButton.Adjustment.Lower = double.MinValue;
			this.WristTableSpinButton.Adjustment.Upper = double.MaxValue;
			
			this.rla_CurrentArm = MyArm;
			this.rla_CurrentArm.PositionUpdated += this.HandleArmPositionUpdated;
			
			this.HandleArmPositionUpdated(this.rla_CurrentArm);
		}

		private void HandleArmPositionUpdated (ArmController Sender)
		{
			RunGtkThread.RunOnGtkThread(()=>{
				if(this.rla_CurrentArm != Sender)
					return;
			
				if(this.bo_DisableEvent)
					return;
				
				this.bo_DisableEvent = true;
				this.XSpinButton.Value = this.rla_CurrentArm.X;
				this.YSpinButton.Value = this.rla_CurrentArm.Y;
				this.ZSpinButton.Value = this.rla_CurrentArm.Z;
				this.WristTableSpinButton.Value = this.rla_CurrentArm.WristTable;
				this.bo_DisableEvent = false;
			});
		}
		
		protected virtual void ValueChangedEvent (object sender, System.EventArgs e)
		{
			if(this.bo_DisableEvent)
				return;
			
			this.bo_DisableEvent = true;
			this.rla_CurrentArm.SetXYZ(this.XSpinButton.Value, this.YSpinButton.Value,
			                           this.ZSpinButton.Value, this.WristTableSpinButton.Value);
			this.bo_DisableEvent = false;
		}
		
	}
}
